package com.mybatis.panel;

import com.mybatis.entity.AwardPunish;
import com.mybatis.entity.StudentVO;
import com.mybatis.service.AwardPunishService;
import com.mybatis.service.StudentService;
import com.mybatis.util.DataUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class AwardPunishPanel extends JPanel {

    public static int pageSize = 1;

    public static String dataRow;

    public static AwardPunish awardPunish = new AwardPunish();

    public static int searchLabel = 0;

    public AwardPunishPanel() {
        setBounds(0, 0, 900, 600);


        //===========================================
//        JPanel panel = new JPanel(new BorderLayout());
//        panel.setBounds(0, 0, 900, 600);//位置
//        panel.add(new JLabel("用户名"));//文字

        JPanel panel2 = new JPanel();
        add(panel2);

        Integer size = 0;
        if (searchLabel == 0)
            size = AwardPunishService.getSize();
        else
            size = AwardPunishService.getSearchSize(awardPunish);


        JLabel jLabel3 = new JLabel();
        jLabel3.setText("共" + size.toString() + "条记录");
        add(jLabel3);

        JLabel jLabel4 = new JLabel();
        int pagesize = size / 28;
        if (size % 28 > 0)
            pagesize++;
        jLabel4.setText("共" + pagesize + "页");
        add(jLabel4);

        JLabel jLabel = new JLabel();
        jLabel.setText("当前页码");
        add(jLabel);

        JLabel jLabel1 = new JLabel();
        Integer page = pageSize;

        jLabel1.setText(page.toString());
        add(jLabel1);

        JButton jButton1 = new JButton("上页");
        add(jButton1);

        JButton jButton2 = new JButton("下页");
        add(jButton2);

        //文本框
        JTextField jTextField = new JTextField(5);
        //panel2.add(jTextField);
        add(jTextField);

        // crud 按钮
        JButton pageButton = new JButton("跳转");
        JButton insertButton = new JButton("添加数据");
        JButton updateButton = new JButton("修改数据");
        JButton deleteButton = new JButton("删除选中");
        add(pageButton);
        add(insertButton);
        add(updateButton);
        add(deleteButton);

        //  多选
        JButton otherButton = new JButton("查看其他信息");
        //panel2.add(jCheckBox);
        //add(otherButton);

        JButton loginButton = new JButton("返回首页");
        add(loginButton);
        JButton button = new JButton("刷新");
        add(button);
        JButton searchButton = new JButton("搜索 | 筛选");
        add(searchButton);

        JLabel searchLabel1 = new JLabel();
        JLabel searchLabel2 = new JLabel();
        JLabel searchLabel3 = new JLabel();
        JLabel searchLabel4 = new JLabel();
        JLabel searchLabel5 = new JLabel();
        JLabel searchLabel6 = new JLabel();
        JLabel searchLabel7 = new JLabel();

        searchLabel1.setText("记录号");
        searchLabel2.setText("学号");
        searchLabel3.setText("");
        searchLabel4.setText("民族");
        searchLabel5.setText("奖惩情况");
        searchLabel6.setText("专业");
        searchLabel7.setText("学院");

        JTextField field1 = new JTextField(5);
        JTextField field2 = new JTextField(5);
        JTextField field3 = new JTextField(5);
        JTextField field4 = new JTextField(5);
        JTextField field5 = new JTextField(5);

        add(searchLabel1);
        add(field1);
        add(searchLabel2);
        add(field2);
        add(searchLabel5);
        add(field3);
//        add(searchLabel6);
//        add(field4);
//        add(searchLabel7);
//        add(field5);

        //表格
        JTable table = new JTable() {
            @Override
            public boolean isCellEditable(int row, int column) {
                //禁止直接编辑表格
                return false;
            }
        };


        Object[] columnNames = {"记录号", "学号", "奖惩情况", "", "", "", ""};

        List<AwardPunish> list;
        if (searchLabel == 0)
            list = AwardPunishService.getPage((pageSize - 1) * 28);
        else
            list = AwardPunishService.getSearchPage((pageSize - 1) * 28, awardPunish);

        Object[][] objects = DataUtil.getConvertData(list);

        DefaultTableModel tableModel = new DefaultTableModel(objects, columnNames);

        //设置表格数据只能单选
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //设置表格数据
        table.setModel(tableModel);

        //panel.add(table);
        //panel.add(table.getTableHeader(), BorderLayout.NORTH);
        add(table.getTableHeader(), BorderLayout.NORTH);
        //把表格内容添加到容器中心
        //panel.add(table, BorderLayout.CENTER);
        add(table, BorderLayout.CENTER);


        //======================按钮事件==============================

        JFrame jFrame1 = new JFrame();
        jFrame1.setSize(300, 300);
        jFrame1.setLayout(null);
        jFrame1.setLocationRelativeTo(null);
        jFrame1.setVisible(false);

        JFrame jFrame2 = new JFrame();
        jFrame2.setSize(300, 300);
        jFrame2.setLayout(null);
        jFrame2.setLocationRelativeTo(null);
        jFrame2.setVisible(false);

        loginButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                MainJFrame.jFrame.setSize(400, 300);
                setPanel(new SelectPanel());
            }
        });

        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                setSearchPanel(false);
                //jFrame1.setVisible(true);
            }
        });

        searchButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String field1Text = field1.getText();
                String field2Text = field2.getText();
                String field3Text = field3.getText();
//                String field4Text = field4.getText();
//                String field5Text = field5.getText();
                awardPunish.setApno(field1Text);
                awardPunish.setSno(field2Text);
                awardPunish.setAprecord(field3Text);
                //studentVO.setMname(field4Text);
                //studentVO.setDname(field5Text);

                setSearchPanel(true);
            }
        });

        jButton1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (pageSize - 1 < 1) {
                    JOptionPane.showMessageDialog(panel2,
                            "当前已在首页!", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                setPanel(--pageSize);
            }
        });

        int pageAll = pagesize;
        jButton2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (pageSize + 1 > pageAll) {
                    JOptionPane.showMessageDialog(panel2,
                            "当前已在末页!", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                setPanel(++pageSize);
            }
        });


        pageButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String text = jTextField.getText();
                if (text == null || text.equals(""))
                    return;
                int parseInt = 0;
                parseInt = Integer.parseInt(text);
                setPanel(parseInt);
            }
        });

        insertButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (MainJFrame.userType == 0) {
                    JOptionPane.showMessageDialog(panel2,
                            "非管理员不可进行操作!", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                jFrame1.setContentPane(new AwardInsertPanel());
                jFrame1.setVisible(true);
                //setPanel(new StudentInsertPanel());
            }
        });

        updateButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (MainJFrame.userType == 0) {
                    JOptionPane.showMessageDialog(panel2,
                            "非管理员不可进行操作!", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                int row = table.getSelectedRow();
                if (row == -1) {
                    JOptionPane.showMessageDialog(panel2,
                            "请选择要修改的行", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                Object value = table.getValueAt(row, 0);
                dataRow = value.toString();
                AwardUpdatePanel.apno = dataRow;
                jFrame2.setContentPane(new AwardUpdatePanel());
                jFrame2.setVisible(true);
               // setPanel(new AwardUpdatePanel());
            }
        });

        deleteButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (MainJFrame.userType == 0) {
                    JOptionPane.showMessageDialog(panel2,
                            "非管理员不可进行操作!", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                int row = table.getSelectedRow();
                if (row == -1) {
                    JOptionPane.showMessageDialog(panel2, "请选择要删除的行", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                dataRow = (String) table.getValueAt(row, 1);
                //System.out.println(value.getClass());
                AwardPunishService.delete(dataRow);
                JOptionPane.showMessageDialog(panel2, "删除成功", "系统提示", JOptionPane.WARNING_MESSAGE);
                setPanel(pageSize);
            }
        });

//        otherButton.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                int row = table.getSelectedRow();
//                if (row == -1) {
//                    JOptionPane.showMessageDialog(panel2, "请选择要查看的行", "系统提示", JOptionPane.WARNING_MESSAGE);
//                    return;
//                }
//                dataRow = (String) table.getValueAt(row, 0);
//                MainJFrame.setPanel(new OtherPanel(0));
//            }
//        });

    }


    public void setPanel(int page) {
        pageSize = page;
        MainJFrame.setPanel(new AwardPunishPanel());
    }

    public void setPanel(JPanel panel) {
        MainJFrame.setPanel(panel);
    }

    public void setSearchPanel(boolean flag) {
        if (flag) searchLabel = 1;
        else searchLabel = 0;
        MainJFrame.setPanel(new AwardPunishPanel());
    }
}
